package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.constant.QbUserConstant;
import com.ruoyi.user.domain.QbUser;
import com.ruoyi.user.service.IQbUserWxService;
import com.ruoyi.user.service.IqbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人员信息Controller
 *
 * @author lzhj
 * @date 2021-01-17
 */
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("qb/user")
public class QbUserController extends BaseController {
    @Autowired
    private IqbUserService qbUserService;

    @Autowired
    private IQbUserWxService qbUserWxService;

    /**
     * 查询人员信息列表
     */

    @ApiOperation("查询人员信息列表,分页。qb:user:list")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", dataType = "int", paramType = "path"),
    })
    @PreAuthorize("@ss.hasPermi('qb:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(QbUser qbUser, SecurityContextHolderAwareRequestWrapper requestWrapper) {
        startPage();
        List<QbUser> list = qbUserService.selectQbUserList(qbUser);
        return getDataTable(list);
    }

    /**
     * 查询人员信息列表
     */

    @ApiOperation("查询人员信息审核列表,分页。qb:user:shlist")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", dataType = "int", paramType = "path"),
    })
    @PreAuthorize("@ss.hasPermi('qb:user:shlist')")
    @GetMapping("/shlist")
    public TableDataInfo shlist(QbUser qbUser) {
        startPage();
        List<QbUser> list = qbUserService.selectQbUserShList(qbUser);
        return getDataTable(list);
    }

    /**
     * 导出人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('qb:user:export')")
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QbUser qbUser) {
        List<QbUser> list = qbUserService.selectQbUserList(qbUser);
        ExcelUtil<QbUser> util = new ExcelUtil<QbUser>(QbUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取人员信息详细信息
     */
    @ApiOperation("获取人员信息详细信息。qb:user:query")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "path")
    @PreAuthorize("@ss.hasPermi('qb:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(qbUserService.selectQbUserById(id));
    }

    /**
     * 新增人员信息
     */
    @ApiOperation("新增人员信息，qb:user:add")
    @PreAuthorize("@ss.hasPermi('qb:user:add')")
    @Log(title = "人员信息", businessType = BusinessType.INSERT)
    @PostMapping
//    @RepeatSubmit
    public AjaxResult add(@RequestBody QbUser qbUser) {
        if (qbUserService.checkRepeat(qbUser)) {
            return AjaxResult.error("证件号、人员类别已存在");
        }
        qbUser.setCreateBy(SecurityUtils.getUsername());
        qbUser.setStatus(QbUserConstant.USER_DSH);
        return toAjax(qbUserService.insertQbUser(qbUser));
    }

    /**
     * 修改人员信息
     */
    @ApiOperation("修改人员信息，qb:user:edit")
    @PreAuthorize("@ss.hasPermi('qb:user:edit')")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbUser qbUser) {

        if (qbUserService.checkRepeat(qbUser)) {
            return AjaxResult.error("证件号、人员类别已存在");
        }
        qbUser.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(qbUserService.updateQbUser(qbUser));
    }

    /**
     * 删除人员信息
     */
    @ApiOperation("删除人员信息，qb:user:remove")
    @ApiImplicitParam(name = "ids", value = "id，多个逗号分隔", required = true, dataType = "string", paramType = "path")
    @PreAuthorize("@ss.hasPermi('qb:user:remove')")
    @Log(title = "人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int i = qbUserService.deleteQbUserByIds(ids);
        return AjaxResult.success("删除成功");
    }

    /**
     * 审核通过
     */
    @ApiOperation("审核通过，qb:user:pass")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids", value = "数据id，数组", dataType = "Long"),
            @ApiImplicitParam(name = "price", value = "价格", dataType = "Double"),
    })
    @PreAuthorize("@ss.hasPermi('qb:user:pass')")
    @Log(title = "审核通过人员信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "pass")
    public AjaxResult passUser(Long[] ids, Double price) {
        if (StringUtils.isNull(ids) && StringUtils.isNull(price)) {
            return AjaxResult.error("参数错误");
        }
        qbUserService.passUser(ids, price);
        return AjaxResult.success("审核成功");
    }

    /**
     * 解绑微信,未更新微信openID的缓存的
     */
    @ApiOperation("解绑微信，根据userid解绑，qb:user:unBindWx")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userid", value = "人员id", dataType = "Long", paramType = "query"),
    })
    @PreAuthorize("@ss.hasPermi('qb:user:unBindWx')")
    @Log(title = "解绑微信", businessType = BusinessType.DELETE)
    @PostMapping(value = "unBindWxByUser/{userid}")
    public AjaxResult unBindWxByUser(@PathVariable Long userid) {
        if (StringUtils.isNull(userid)) {
            return AjaxResult.error("参数错误");
        }
        int i = qbUserWxService.unBindWx(userid);
        return AjaxResult.success("解绑成功");
    }

    /**
     * 删除人员-微信绑定关系
     */
    @ApiOperation("删除人员-微信绑定关系，qb:userwx:unBindWx")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids", value = "微信绑定id，多个逗号分隔", dataType = "Long"),
    })
    @PreAuthorize("@ss.hasPermi('qb:userwx:unBindWx')")
    @Log(title = "人员-微信绑定关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/unBindWx/{ids}")
    public AjaxResult unBindWx(@PathVariable Long[] ids)
    {
        if (ids == null){
            return AjaxResult.error("人员未绑定微信");
        }
        int i = qbUserWxService.deleteQbUserWxByIds(ids);
        return AjaxResult.success("解绑成功");
    }


}
